/**
 * JAVACC DEMO 1.0
 */
package com.apache.passport.controller;

import com.apache.action.PlatFromAction;
import com.apache.api.manager.BaseManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultMsg;
import com.apache.database.constant.Validator;
import com.apache.database.model.Page;
import com.apache.passport.common.DesUtils;
import com.apache.passport.common.PassPortConst;
import com.apache.passport.common.PassportHelper;
import com.apache.passport.entity.BlackRoster;
import com.apache.passport.entity.Token;
import com.apache.passport.manager.BlackRosterManager;
import com.apache.tools.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description:  黑名单管理
 *
 * @author Hou Dayu 创建时间：2016-12-24
 */
@Controller
@RequestMapping({ "/passport/black/" })
public class BlackRosterAction extends PlatFromAction<BlackRoster> {

    private final String BLACK_LIST = getPrefix() + "black-list";

    private final String BLACK_SAVE = getPrefix() + "black-save";

    @Autowired
    private BlackRosterManager blackRosterManager;

    @RequestMapping({ "black-list.action" })
    public ModelAndView initList(HttpServletRequest request) {
        ModelAndView view = new ModelAndView(BLACK_LIST);
        return view;
    }

    @RequestMapping({ "black-save.action" })
    public ModelAndView save(HttpServletRequest request) {
        ModelAndView view = new ModelAndView(BLACK_SAVE);
        return view;
    }

    /**
     * TODO 执行用户修改之前,需要对数据进行反显,故首先获取数据值
     *
     * @see com.apache.action.PlatFromAction#edit(javax.servlet.http.HttpServletRequest, java.lang.String)
     */
    @RequestMapping({ "edit.action" })
    public ModelAndView edit(HttpServletRequest request, String sysId) {
        ParamsVo vo = new ParamsVo();
        vo.setInfoId(sysId);
        ModelAndView view = new ModelAndView(BLACK_SAVE);
        view.addObject("item", blackRosterManager.getInfoById(vo));
        view.addObject("isAdd", "false");
        return view;
    }

    /**
     * TODO 执行用户删除操作
     *
     * @see com.apache.action.PlatFromAction#delete(javax.servlet.http.HttpServletRequest, java.lang.String)
     */
    @RequestMapping({ "del.action" })
    @ResponseBody
    public ResultMsg delete(HttpServletRequest request, String sysId) {
        ResultMsg rmsg = new ResultMsg("F", "操作失败,请重试!");
        try {
            String remoteIp = request.getRemoteAddr();
            String userEname = request.getParameter("userEname");
            if (StrUtil.isNotNull(sysId)) {
                ParamsVo auVo = new ParamsVo();
                BlackRoster au = new BlackRoster();
                au.setBrId(sysId);
                auVo.setObj(au);
                List currSys = blackRosterManager.getList(auVo);
                if (Validator.isEmpty(currSys)) {
                    rmsg.setFlag("H");
                    rmsg.setMsg("当前IP不存在");
                } else {
                    ParamsVo asVo = new ParamsVo();
                    asVo.setInfoId(sysId);
                    blackRosterManager.deleteInfo(asVo);
                    PassPortConst.detLonErrToken(remoteIp, userEname);
                    rmsg.setFlag("T");
                    rmsg.setMsg("删除成功");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rmsg;
    }

    @RequestMapping(value = { "save.action" }, method = {
            org.springframework.web.bind.annotation.RequestMethod.POST })
    @ResponseBody
    public ResultMsg save(HttpServletRequest request, HttpServletResponse response,
            BlackRoster blackRoster) {
        ResultMsg rmsg = new ResultMsg("T", "操作成功!");
        String isAdd = getDefaultStr(request.getParameter("isAdd"), "true");
        ParamsVo<BlackRoster> vo = new ParamsVo<BlackRoster>();
        String cookieValue = PassportHelper.getInstance().getCurrCookie(request);
        Token token = PassPortConst.getToken(cookieValue);
        String currEname = DesUtils.getInstance().decrypt(token.getUserEname());
        blackRoster.setUserEname(Validator.isNull(currEname) ? "" : currEname);//添加系统人名称
        String nowDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        blackRoster.setBrCreatetime(nowDate);
        //如果系统Id为空

        vo.setObj(blackRoster);
        String id = "";
        boolean mark = false;
        if ("false".equals(isAdd))
            mark = this.blackRosterManager.editInfo(vo);
        else {
            id = this.blackRosterManager.saveInfo(vo);
        }
        boolean a1 = (("false".equals(isAdd)) && (!(mark)));
        boolean a2 = (("true".equals(isAdd)) && (isNull(id)));
        if (a1 || a2) {
            rmsg.setFlag("F");
            rmsg.setMsg("操作失败！");
        }
        return rmsg;
    }

    /**
     * description:  查询黑名单列表
     *
     * @param request
     * @return
     */
    @RequestMapping({ "list!data.action" })
    @ResponseBody
    public Object list(HttpServletRequest request, String page, String rows) {
        ParamsVo<BlackRoster> vo = new ParamsVo<BlackRoster>();
        BlackRoster blackRoster = new BlackRoster();
        vo.setParams("pageSize", Validator.getDefaultStr(rows, "20"));
        vo.setParams("pageIndex", Validator.getDefaultStr(page, "1"));
        vo.setObj(blackRoster);
        Page pageList = blackRosterManager.getPageInfo(vo);
        Map<String, Object> mapData = new HashMap<String, Object>();
        mapData.put("total", Integer.valueOf(pageList.getCount()));
        mapData.put("rows", pageList.getPageObjects());
        return mapData;
    }

    @Override
    protected Object data(HttpServletRequest arg0, String arg1, String arg2) {
        return null;
    }

    @Override
    protected BaseManager<BlackRoster> getBaseManager() {
        return null;
    }

    @Override
    protected String getPrefix() {
        return "/passport/passport-";
    }

}
